/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hiddenwebsearch;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Star
 */
public class URLParser {

    //Initial the urlList and size of a given query
    private List<String> urlList = new ArrayList<String>();
    private int size = 0;

    //Parse the return XML
    public void parserXml(String url) {
        //reset size and urlList
        size = 0;
        urlList.clear();
        //Temp variable
        int index = 0;
        String tag = "";
        //Parse xml
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(url);
            //get the children of the root
            NodeList rsp = document.getChildNodes();
            Node rinfo = rsp.item(index);
            tag = rinfo.getNodeName();
            while (tag != "SearchResponse") {
                index++;
                rinfo = rsp.item(index);
                tag = rinfo.getNodeName();
            }
            //get total value
            Node ww = this.getNodesbyName(rinfo, "web:Web");
            Node ts = this.getNodesbyName(ww, "web:Total");
            size = Integer.parseInt(ts.getTextContent());
            if(size>0)
            {
            Node wrs = this.getNodesbyName(ww, "web:Results");
            NodeList wlist = wrs.getChildNodes();
            //get result url
            for (index = 0; index < wlist.getLength(); index++) {
                Node ws = wlist.item(index);
                Node urls = this.getNodesbyName(ws, "web:Url");
                urlList.add(urls.getTextContent());
            }
            }
            else
                urlList.clear();

        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (ParserConfigurationException e) {
            System.out.println(e.getMessage());
        } catch (SAXException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
    //return size
    public int getSize() {
        return size;
    }
    //return Urls
    public List<String> getUrls() {
        return urlList;
    }
    //find node by name
    private Node getNodesbyName(Node parent, String tag) {
        String ctag = "";
        int i = 0;
        NodeList cnodes = parent.getChildNodes();
        ctag = cnodes.item(i).getNodeName();
        while (!ctag.equals(tag)) {
            i++;
            ctag = cnodes.item(i).getNodeName();
        }
        return cnodes.item(i);
    }
}
